import * as f2e3 from 'f2e-server3'

export type DBKey = string;

/** 登录用户信息 */
export interface User {
    /** 登录用户名 */
    username: string;
    /** 用户展示名称 */
    nickname?: string;
    /** 用户密码 */
    password?: string;
    /** 用户角色 */
    roles: UserRole['key'][];
    rolesInfo?: UserRole[];
    /** 角色权限 */
    permissions?: Set<UserPermission['key']>;
    /** 角色只读不可删除 */
    readonly?: boolean;
}

/** 用户角色信息 */
export interface UserRole {
    /** 权限唯一ID */
    key: DBKey;
    /** 角色名称 */
    name: string;
    /** 角色描述 */
    desc?: string;
    /** 角色权限 */
    permissions: UserPermission['key'][];
    /** 角色只读不可删除 */
    readonly?: boolean;
}

/** 基础权限信息 */
export interface UserPermission {
    /** 权限唯一ID */
    key: DBKey;
    /** 权限名称 */
    name: string;
    /** 权限描述 */
    desc?: string;
    /** 权限只读不可删除 */
    readonly?: boolean;
}

/** 内置权限项
 * 需要跟初始化数据同步 */
export enum PermissionKey {
    RBAC_PERMISSION = 'rbac_permission',
    RBAC_ROLE = 'rbac_role',
    RBAC_USER = 'rbac_user',
    RBAC_SELF = 'rbac_self'
}
/** HTTP接口服务相关定义 */
export interface PageRecord {
    page: number;
    pageSize: number;
}
export interface PageParams<T> extends Partial<PageRecord> {
    params?: T;
}
export interface Pager<T> extends PageRecord {
    total: number;
    list: T[];
}
export interface ServerResult<T> {
    success: boolean;
    msg?: string;
    data?: T;
}
export interface FetchAPI<F = never, T = never> {
    (body: F): Promise<ServerResult<T>>;
}

/** 自定义持久化接口定义 */
export interface UserRoleService {
    roleCreate: FetchAPI<UserRole[]>;
    roleDelete: FetchAPI<UserRole['key'][]>;
    roleUpdate: FetchAPI<Partial<UserRole> & { key: UserRole['key'] }>;
    roleList: FetchAPI<PageParams<Partial<UserRole>>, Pager<UserRole>>;
    
    getUserRoleByKey: {(key: UserRole['key']): Promise<UserRole | undefined>};
}
export interface UserPermissionService {
    permissionCreate: FetchAPI<UserPermission[]>;
    permissionDelete: FetchAPI<UserPermission['key'][]>;
    permissionUpdate: FetchAPI<Partial<UserPermission> & { key: UserPermission['key'] }>;
    permissionList: FetchAPI<PageParams<Partial<UserPermission>>, Pager<UserPermission>>;
}
export interface UserService extends f2e3.IUserStore {
    userCreate: FetchAPI<User[]>;
    userDelete: FetchAPI<User['username'][]>;
    userUpdate: FetchAPI<Partial<User> & { username: User['username'] }>;
    userList: FetchAPI<PageParams<Partial<User>>, Pager<User>>;

    getUserByUsername: {(username: User['username']): Promise<User | undefined>};
}

interface InitData {
    permissions: UserPermission[];
    roles: UserRole[];
    users: User[];
}
export interface ServiceAll {
    /** 用户角色服务 */
    userRoleService: UserRoleService;
    /** 用户权限服务 */
    userPermissionService: UserPermissionService;
    /** 用户服务 */
    userService: UserService;
    /** 初始化: 内部实现判断并进行数据库初始化操作 */
    init: (data: InitData) => Promise<void>;
}
export interface ServiceOptions {
    /**
     * @default "/admin"
     */
    prefix?: string;
    /**
     * 登出地址, 对应 auth 中配置
     */
    logout_url?: string;
    /**
     * 提供页面访问
     * 默认：true, 设置为 false 时，将不提供页面访问，仅支持接口
     */
    load_page?: boolean;
}